package com.fleafinder.xmlparser;

import java.io.IOException;
import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

import com.fleafinder.CommonConstants;


public class GoogleMapXmlParser {
	public static final String TAG_LOCATION = "location";
	public static final String TAG_ADD_LAT = "lat";
	public static final String TAG_ADD_LNG = "lng";
	
	public static final int I_TAG_LOCATION = 0;
	public static final int I_TAG_ADD_LAT = 1;
	public static final int I_TAG_ADD_LNG = 2;
	
	private int currentTag = -1;
	
	private double latitude, longtitude;
	
	/**
	 * 
	 * @param dataHolder
	 */
	public GoogleMapXmlParser(){
		
	}
	
	/**
	 * Receives an InputStream.<br/>
	 * After processing, this will closes the received InputStream.
	 * 
	 * @param in
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	public void parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, CommonConstants.ENCODING_UTF_8);
            readLocation(parser);
            
        } catch(Exception e){
        	e.printStackTrace();
        } finally {
            in.close();
        }
    }

	
	/**
	 * @param parser
	 * @param m
	 * @return
	 * @throws XmlPullParserException
	 * @throws IOException
	 */
	private void readLocation(XmlPullParser parser)
			throws IOException {
		try {
			parser.next();
			boolean isInsideTagLocation = false;
			
			while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {

				int next = parser.getEventType();

				if (next == XmlPullParser.END_TAG) {
					if(currentTag == I_TAG_LOCATION && isInsideTagLocation){
						break;
					} else {
						currentTag = I_TAG_LOCATION;
					}
				} else if (next == XmlPullParser.START_TAG) {
					if (TAG_LOCATION.equals(parser.getName())) {
						isInsideTagLocation = true;
					} else if (TAG_ADD_LAT.equals(parser.getName())) {
						currentTag = I_TAG_ADD_LAT;
					} else if (TAG_ADD_LNG.equals(parser.getName())) {
						currentTag = I_TAG_ADD_LNG;
					}
				} else if (next == XmlPullParser.TEXT) {

					// TODO: check empty string

					if (currentTag == I_TAG_LOCATION) {
//						m.setId(Integer.parseInt(parser.getText()));
					} else if (currentTag == I_TAG_ADD_LAT) {
//						m.setName(parser.getText());
						this.latitude = Double.parseDouble(parser.getText());
					} else if (currentTag == I_TAG_ADD_LNG) {
						//m.setMarketType(parser.getText());
//						m.setMarketTypeName(parser.getText());
						this.longtitude = Double.parseDouble(parser.getText());
					}
				}
				parser.next();
			}
			
		} catch (XmlPullParserException ex) {
			ex.printStackTrace();
		}
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}

	public double getLongtitude() {
		return longtitude;
	}

	public void setLongtitude(double longtitude) {
		this.longtitude = longtitude;
	}
}
